#!/usr/bin/env python3
"""
修复FFmpeg错误日志记录
"""

import re
from pathlib import Path

def fix_video_splitter_logging():
    """修复video_splitter.py中的错误日志记录"""
    
    file_path = Path("app/services/video_splitter.py")
    
    if not file_path.exists():
        print(f"❌ 文件不存在: {file_path}")
        return
    
    # 读取文件内容
    with open(file_path, 'r', encoding='utf-8') as f:
        content = f.read()
    
    # 查找并替换异常处理部分
    old_pattern = r'        except Exception as e:\r?\n            logger\.error\(f"视频片段提取失败: \{e\}"\)\r?\n            return False'
    
    new_code = '''        except Exception as e:
            logger.error(f"视频片段提取失败: {e}")
            import traceback
            logger.error(f"详细错误信息: {traceback.format_exc()}")
            return False'''
    
    # 执行替换
    new_content = re.sub(old_pattern, new_code, content)
    
    if new_content != content:
        # 写回文件
        with open(file_path, 'w', encoding='utf-8') as f:
            f.write(new_content)
        print(f"✅ 已修复 {file_path} 的错误日志记录")
    else:
        print(f"⚠️ 未找到需要修复的代码模式")
        
        # 手动查找异常处理位置
        lines = content.split('\n')
        for i, line in enumerate(lines):
            if 'except Exception as e:' in line and '视频片段提取失败' in lines[i+1]:
                print(f"找到异常处理位置: 第{i+1}行")
                print(f"当前代码: {lines[i:i+3]}")
                break

if __name__ == "__main__":
    fix_video_splitter_logging()